package com.example.chapter05;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter05.database.UserDBHelper;
import com.example.chapter05.entity.User;
import com.example.chapter05.util.ViewUtil;

import java.util.Random;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_register_phone;
    private EditText et_register_password;
    private EditText et_register_password_again;
    private EditText et_verifyCode;
    private UserDBHelper mHelper;
    private String mVerifyCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        et_register_phone = findViewById(R.id.et_register_phone_number);
        et_register_password = findViewById(R.id.et_register_password);
        et_register_password_again = findViewById(R.id.et_register_password_again);
        et_verifyCode = findViewById(R.id.et_verifyCode);

        findViewById(R.id.btn_get_verifyCode).setOnClickListener(this);
        findViewById(R.id.btn_register).setOnClickListener(this);

        et_register_phone.addTextChangedListener(new HideTextWatcher(et_register_phone, 11));
        et_register_password.addTextChangedListener(new HideTextWatcher(et_register_password, 6));
        et_register_password_again.addTextChangedListener(new HideTextWatcher(et_register_password_again, 6));
        et_verifyCode.addTextChangedListener(new HideTextWatcher(et_verifyCode, 6));

    }

    @Override
    protected void onStart() {
        super.onStart();
        mHelper = UserDBHelper.getInstance(this);
        mHelper.openWriteLink();
        mHelper.openReadLink();
    }

    @Override
    protected void onStop() {
        super.onStop();
        //mHelper.closeLink();
    }

    @Override
    public void onClick(View v) {
        String phone = et_register_phone.getText().toString();
        String password = et_register_password.getText().toString();
        String confirm_password = et_register_password_again.getText().toString();
        String inputVerifyCode = et_verifyCode.getText().toString();
        if (v.getId() == R.id.btn_get_verifyCode) {
            //判断登录密码和确认密码是否一致，不一致将两者清空
            if (!password.equals(confirm_password)) {
                et_register_password.setText(null);
                et_register_password_again.setText(null);
                Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }
            //两者相等,弹窗显示
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("请记住验证码");
            mVerifyCode = String.format("%06d", new Random().nextInt(999999));
            String desc = String.format("当前手机号码：%s,密码为：%s,当前的验证码为：%s", phone, password,
                    mVerifyCode);
            builder.setMessage(desc);
            builder.create().show();
        }
        if (v.getId() == R.id.btn_register) {

            if (!inputVerifyCode.equals(mVerifyCode)) {
                Toast.makeText(this, "验证码输入错误", Toast.LENGTH_SHORT).show();
                return;
            }
            //Log.d("liu","开始添加数据到数据库");
            //将数据添加到数据库中
            User user = new User();
            user.setPassword(password);
            user.setPhone(phone);
            //Log.d("liu",user.toString());
            if (mHelper.insert(user) > 0) {
                Log.d("liu","插入成功");
            }else {
                //Log.d("liu","插入失败");
                Toast.makeText(this,"当前用户已存在",Toast.LENGTH_SHORT).show();
                return;
            }

            //带着新的账号和密码返回主界面
            /*
            *Intent intent = new Intent(this,LoginMainActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("phone",phone);
            intent.putExtras(bundle);
            startActivity(intent);*/
            //回到main界面
            Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show();
            finish();
        }

    }

    private void insertDatabase() {

    }

    private class HideTextWatcher implements TextWatcher {
        private final EditText mView;
        private final int maxLength;

        public HideTextWatcher(EditText et, int maxLength) {
            this.maxLength = maxLength;
            this.mView = et;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() == maxLength) {
                ViewUtil.hideOneInputMethod(RegisterActivity.this, mView);
            }

        }
    }
}